//
//  GesturesToUnlockViewController.m
//  CoreLock
//
//  Created by QA on 15/9/12.
//  Copyright (c) 2015年 冯成林. All rights reserved.
//

#import "GesturesToUnlockViewController.h"
#import "CoreLockConst.h"
#import "CoreArchive.h"
#import "CLLockLabel.h"
#import "CLLockNavVC.h"
#import "CLLockView.h"
#import "FingerprintLock.h"

#import "MainBodyViewController.h"
#import "ExitLoginService.h"

@interface GesturesToUnlockViewController ()

@property (nonatomic,copy) void (^successBlock)(GesturesToUnlockViewController *lockVC,NSString *pwd);

@property (weak, nonatomic) IBOutlet CLLockLabel *label;

@property (weak, nonatomic) IBOutlet CLLockView *lockView;

@property (nonatomic,strong) UIBarButtonItem *resetItem;

@property (nonatomic,copy) NSString *modifyCurrentTitle;

/** 直接进入修改页面的 */
@property (nonatomic,assign) BOOL isDirectModify;

@end

@implementation GesturesToUnlockViewController

+ (void)showGesturesToUnlockPushView:(UINavigationController*)navigationController
                           block:(void(^)(GesturesToUnlockViewController *lockVC, NSString *pwd))successBlock
{
    GesturesToUnlockViewController *lockVC = [[GesturesToUnlockViewController alloc] init];
    lockVC.successBlock = successBlock;
    [navigationController pushViewController:lockVC animated:YES];
}

+ (void)showGesturesToUnlockPresentView:(UINavigationController*)navigationController
                           block:(void(^)(GesturesToUnlockViewController *lockVC, NSString *pwd))successBlock
{
    UIViewController *viewController = navigationController.visibleViewController;
    if (![viewController isKindOfClass:[GesturesToUnlockViewController class]]) {
        GesturesToUnlockViewController *lockVC = [[GesturesToUnlockViewController alloc] init];
        lockVC.successBlock = successBlock;
        [lockVC verificationPasscode]; //开启指纹验证
        
        BOOL isAnimated = viewController;
        UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:lockVC];
        [navigationController presentViewController:navVC animated:isAnimated completion:nil];
    } else {
        GesturesToUnlockViewController *lockVC = (GesturesToUnlockViewController*) viewController;
        [lockVC verificationPasscode]; //开启指纹验证
    }
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    
    self.lockView.type = CoreLockTypeVeryfiPwd;
    [self.label showNormalMsg:CoreLockVerifyNormalTitle];
    
    //移除键盘事件
    [self.view removeGestureRecognizer:self.tapGr];
    [self.view removeGestureRecognizer:self.swipeGr];
    
    //控制器准备
    [self vcPrepare];
    
    //事件
    [self event];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    if (!self.navigationController.navigationBarHidden)
        [self.navigationController setNavigationBarHidden:YES animated:YES];
}

//指纹验证
- (void)verificationPasscode
{
    [FingerprintLock verificationPasscodeCompletion:^(BOOL success, NSError *error) {
        if (success) if(_successBlock != nil) _successBlock(self, nil);
    }];
}


/*
 *  事件
 */
-(void)event {
    
    /** 开始 */
    self.lockView.verifyPWBeginBlock = ^(){
        [self.label showNormalMsg:CoreLockVerifyNormalTitle];
    };
    
    /** 密码长度不够 */
    self.lockView.setPWSErrorLengthTooShortBlock = ^(NSUInteger currentCount){
        [self.label showWarnMsg:[NSString stringWithFormat:@"输入密码错误"]];
    };
    
    /** 验证 */
    self.lockView.verifyPwdBlock = ^(NSString *pwd){
        //取出本地密码
        NSString *pwdLocal = [CoreArchive strForKey:CoreLockPWDKey];
        BOOL res = [pwdLocal isEqualToString:pwd];
        
        if(res){//密码一致
            [self.label showNormalMsg:CoreLockVerifySuccesslTitle];
            self.view.userInteractionEnabled = NO;
            if(_successBlock != nil) _successBlock(self,pwd);
            
        } else {//密码不一致
            [self.label showWarnMsg:CoreLockVerifyErrorPwdTitle];
        }
        
        return res;
    };
    
}

/*
 *  控制器准备
 */
-(void)vcPrepare{
    
    //设置背景色
    self.view.backgroundColor = ThemeColorBlue;
    self.navigationController.navigationBarHidden = YES;
    
    //初始情况隐藏
    self.navigationItem.rightBarButtonItem = nil;
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    //默认标题
    self.modifyCurrentTitle = CoreLockModifyNormalTitle;
}

-(void)dismiss{
    [self dismiss:0];
}

/*
 *  是否有本地密码缓存？即用户是否设置过初始密码？
 */
+ (BOOL)hasPwd {
    
    NSString *pwd = [CoreArchive strForKey:CoreLockPWDKey];
    
    return pwd !=nil;
}

/*
 *  消失
 */
-(void)dismiss:(NSTimeInterval)interval{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];
    });
}

/*
 *  重置
 */
-(UIBarButtonItem *)resetItem {
    
    if(_resetItem == nil){
        //添加右按钮
        _resetItem= [[UIBarButtonItem alloc] initWithTitle:@"重设" style:UIBarButtonItemStylePlain target:self action:@selector(setPwdReset)];
    }
    
    return _resetItem;
}

/*
 *  密码重设
 */
-(void)setPwdReset {
    
    [self.label showNormalMsg:CoreLockPWDTitleFirst];
    
    //隐藏
    self.navigationItem.rightBarButtonItem = nil;
    
    //通知视图重设
    [self.lockView resetPwd];
}

- (IBAction)forgetPwdAction:(id)sender
{
    //关闭手势功能
    [ExitLoginService removePasswdLock];
    [ExitLoginService exitAppUserLogin];
}

- (IBAction)fingerprintPwdAction:(id)sender
{
    [FingerprintLock showPasscodeCompletion:^(BOOL success, NSError *error) {
        if (success) if(_successBlock != nil) _successBlock(self, nil);
        NSLog(@"指纹验证%@", success ? @"成功" : @"失败");
    }];
}

@end

